package com.example.forjava.handle;

import com.example.forjava.exception.DoStepException;
import com.example.forjava.exception.OneException;
import com.example.forjava.exception.TwoException;
import com.example.forjava.exception.ValidateException;
import com.example.forjava.util.GenericJsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class GlobalControllerExceptionHandle {

    @ExceptionHandler(ValidateException.class)
    public GenericJsonResult validateException(HttpServletRequest request, Exception validate) {
        String url = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        String authType = request.getAuthType();
        String contextPath = request.getContextPath();
        String method = request.getMethod();
        String pathInfo = request.getPathInfo();
        String pathTranslated = request.getPathTranslated();
        String queryString = request.getQueryString();
        return GenericJsonResult.fail("500", url + ">>>>" + "ValidateException.class");
    }

    @ExceptionHandler(DoStepException.class)
    public GenericJsonResult doStepException(HttpServletRequest request, Exception validate) {
        String url = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        String authType = request.getAuthType();
        String contextPath = request.getContextPath();
        String method = request.getMethod();
        String pathInfo = request.getPathInfo();
        String pathTranslated = request.getPathTranslated();
        String queryString = request.getQueryString();
        return GenericJsonResult.fail("500", url + ">>>>" + "DoStepException.class");
    }

    @ExceptionHandler(Throwable.class)
    public GenericJsonResult throwable(HttpServletRequest request, Throwable validate) {
        String url = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        String authType = request.getAuthType();
        String contextPath = request.getContextPath();
        String method = request.getMethod();
        String pathInfo = request.getPathInfo();
        String pathTranslated = request.getPathTranslated();
        String queryString = request.getQueryString();
        return GenericJsonResult.fail("500", url + ">>>>" + "Throwable.class");
    }

    @ExceptionHandler({OneException.class, TwoException.class})
    public GenericJsonResult demo(HttpServletRequest request, Exception validate) {
        String url = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        String authType = request.getAuthType();
        String contextPath = request.getContextPath();
        String method = request.getMethod();
        String pathInfo = request.getPathInfo();
        String pathTranslated = request.getPathTranslated();
        String queryString = request.getQueryString();
        return GenericJsonResult.fail("500", url + ">>>>" + "oneAndTwo.class");
    }



}
